/**
 * @description 预支付订单请求
 */
export default {
  data() {
    return {}
  },
  methods: {
    /**
     * @description   步骤1：确认支付
     */
    submitPay() {
      if (this.options.orderType != "film" && this.options.preInfo.needPay == 0) {
        // 非影票支付
        this.perPay()
      } else if (this.options.orderType == "film" && this.needPay == 0 && !this.options.preInfo?.checkVipCardV2Info?.ruleId) {
        // 影票支付且没有选中会员卡
        this.perPay()
      } else {
        if (this.payType) {
          this.perPay()
        } else if (this.payWayList.length == 0) {
          uni.$toast("当前无可用支付方式")
        } else {
          uni.$toast("请选择支付方式")
        }
      }
    },
    /**
     * @description 步骤2： 判断订单类型准备预支付下单
     */

    perPay() {
      const orderType = this.options.orderType
      // 继续支付
      if (this.options.isContinuePay) {
        this.handleContinuePay()
        return
      }
      if (orderType == "film") {
        // 影票
        const preData = this.createdPrePayDate()
        this.orderFilmPerPay(preData)
      } else if (orderType == "goods") {
        // 卖品
        this.orderGoodsPerPay()
      } else if (orderType == "mall") {
        // 套票
        this.orderMallPerPay()
      } else if (orderType == "accoutRecharge") {
        // 余额充值
        this.orderBalancePerPay()
      } else if (orderType == "cardRecharge") {
        // 卡充值
        this.orderCardPerPay()
      }
    },
    // 步骤3： 生成预下单
    // 影片预下单接口参数处理
    createdPrePayDate() {
      const cardInfo = this.preInfo?.film?.cardInfo || {}
      const coupons = this.preInfo?.film?.coupons || []
      let couponIds = coupons
        .map((i) => i.couponId)
        .filter((i) => i)
        .join(",")
      let vipCardV2Info = {}
      if (this.preInfo?.film?.vipCardPrice > 0) {
        vipCardV2Info = { ruleId: this.preInfo.film.checkVipCardV2Info.ruleId, vipCardId: this.preInfo.film.checkVipCardV2Info.vipCardId }
      }
      if (this.preInfo?.film?.saleOrderNo) {
        vipCardV2Info.saleOrderNo = this.preInfo.film.saleOrderNo
        couponIds = ""
      }

      const data = {
        vipCardV2Info: JSON.stringify(vipCardV2Info),
        orderId: this.preInfo.orderId,
        couponIds,
        integral: String(this.calcMaxIntegralCut()), // 使用积分
        balance: String(this.caclBalanceCut()), // 使用余额
        prePrice: String(this.needPay), // 前端预算剩余支付款项
        cardInfo: JSON.stringify(cardInfo), // 使用卡
        goodsOrderId: "", // 卖品订单ID
        goodsCouponIds: "", // 卖品券ID
        realNameIds: "" // 实名
      }
      return data
    },
    // 影片预下单
    orderFilmPerPay(preData) {
      uni.$api.orderFilm.prePay(preData, { loading: true }).then((res) => {
        const preResult = { orderId: res.orderId, payMoney: res.payPrice }
        if (this.isNaNStr(res.payPrice) > 0) {
          if (uni.$uniPlatform === "web") {
            this.webCreateOrderPay({ ...preResult, orderType: this.options.orderType })
          } else {
            this.createOrderPay({ ...preResult })
          }
        } else {
          uni.$setCache("payStatus", 1)
          this.paySuccess({ ...preResult, orderType: "film" })
        }
      })
    },
    // 余额充值预下单
    orderBalancePerPay() {
      // v3.0.1 版本
      uni.$api.balance.createOrder({ rechargeMoney: this.needPay, version: "v2", discountMoney: this.options.showNeedPay }).then((res) => {
        if (this.isNaNStr(res.payPrice) > 0) {
          if (uni.$uniPlatform === "web") {
            this.webCreateOrderPay({ orderId: res.orderId, payMoney: res.payPrice, orderType: this.options.orderType })
          } else {
            this.createOrderPay({ orderId: res.orderId, payMoney: res.payPrice })
          }
        }
      })
    },
    // 卡充值预下单
    orderCardPerPay() {
      uni.$api.card.cardRecharge({ rechargeMoney: this.needPay, ...this.options.query }).then((res) => {
        if (this.isNaNStr(res.payPrice) > 0) {
          if (uni.$uniPlatform === "web") {
            this.webCreateOrderPay({ orderId: res.orderId, payMoney: res.payPrice, orderType: this.options.orderType })
          } else {
            this.createOrderPay({ orderId: res.orderId, payMoney: res.payPrice })
          }
        }
      })
    },

    orderGoodsPerPay() {},
    orderMallPerPay() {
      if (uni.$uniPlatform === "web") {
        this.webCreateOrderPay({ orderId: this.options.orderId, payMoney: this.options.preInfo.needPay, orderType: this.options.orderType })
      } else {
        this.createOrderPay({ orderId: this.options.orderId, payMoney: this.options.preInfo.needPay })
      }
    }
  }
}
